package com.safewind.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.safewind.dao.HistorySendDao;
import com.safewind.model.HistorySend;
import com.safewind.service.IHistorySendService;
import com.safewind.util.StringUtil;

@Service("historySendService")
public class HistorySendServiceImpl implements IHistorySendService {

	@Resource
	private HistorySendDao historySendDao;

	@Override
	public void addSend(String userId, String content, int number, int cost) {
		StringBuffer sb = new StringBuffer();
		sb.append("发送消息，收件人：");
		sb.append(number);
		sb.append("人，花费：");
		sb.append(cost);
		addHistorySend(userId, content, sb);
	}

	@Override
	public void addQuartz(String userId, String content, int number, int cost) {
		StringBuffer sb = new StringBuffer();
		sb.append("添加定时任务，收件人：");
		sb.append(number);
		sb.append("人，花费：");
		sb.append(cost);
		addHistorySend(userId, content, sb);
	}

	@Override
	public void dealQuartz(String userId, String content, int reply) {
		StringBuffer sb = new StringBuffer();
		sb.append("执行定时任务");
		if (reply != 1) {
			sb.append("失败,已退回费用。");
			String answer = StringUtil.analysisReply(reply);
			sb.append("失败原因:" + answer);
		} else {
			sb.append("成功");
		}
		addHistorySend(userId, content, sb);

	}

	@Override
	public void cancelQuartz(String userId, String content) {
		StringBuffer sb = new StringBuffer();
		sb.append("取消定时任务");
		addHistorySend(userId, content, sb);
	}

	private void addHistorySend(String userId, String content, StringBuffer sb) {
		HistorySend hs = new HistorySend();
		hs.setPkId(StringUtil.getUUID());
		hs.setContent(sb.toString());
		hs.setSendContent(content);
		hs.setUserId(userId);
		hs.setCreateTime(new Date());
		historySendDao.insert(hs);
	}

	@Override
	public List<HistorySend> getAll(String userId) {
		return historySendDao.getAll(userId);
	}

}
